<template>
  <div>
    <div class="app_card mb-2 pt-4 pb-4">
      <div class="d-flex justify-content-between search-form">
        <div class="flex-grow-1">
          <el-radio-group class="radio_button mr-3" v-model="radioFloor" @change="floorChange">
            <el-radio-button label="-1">审核中</el-radio-button>
            <el-radio-button label="-2">审核记录</el-radio-button>
          </el-radio-group>
          <div class="el-select w250px" style="margin-top: 2px;">
            <el-date-picker style="width:220px" v-model="queryParam.time" type="daterange" range-separator="To" start-placeholder="开始时间" end-placeholder="结束时间" />
          </div>
          <el-input class="w200px mr-3" v-model.trim="queryParam.name" autocomplete="off" placeholder="课程名称"></el-input>
          <el-input class="w200px mr-3" v-model.trim="queryParam.name" autocomplete="off" placeholder="发布者"></el-input>
          <el-select class="w100px mr-3" v-model="queryParam.search" placeholder="所属教室">
            <el-option label="教室1" value="1" />
            <el-option label="教室2" value="2" />
          </el-select>
          <el-select class="w100px mr-3" v-model="queryParam.pullingState" placeholder="失败原因">
            <el-option v-for="item in releaseStateOption" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-button type="primary"  @click="onRefresh(null)">搜索</el-button>
          <el-button type="primary" plain @click="onReset()">重置</el-button>
        </div>
        <div class="d-flex">
          <el-button type="primary" plain @click="onRow(null)">批量通过</el-button>
          <el-button type="primary" plain @click="onPass(null)">批量不通过</el-button>
        </div>
      </div>
    </div>
    <div class="app_card pt-4  pb-5">
      <el-table stripe size="large" :data="dataOfTable" height="calc(100vh - 300px)" @selection-change="handleSelectionChange">
        <el-table-column fixed type="selection" width="50" />
        <el-table-column label="封面" prop="image" align="center" :minWidth="80" show-overflow-tooltip>
          <template #default="scope">
            <el-image preview-teleported="true" style="height: 30px" :src="scope.row.image" :zoom-rate="1.2" :max-scale="7" :min-scale="0.2" :preview-src-list="[scope.row.image]" :initial-index="0" fit="cover" />
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" align="center" :minWidth="100" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.title }}</template>
        </el-table-column>
        <el-table-column label="课程名称" prop="courseName" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.Course.name }}</template>
        </el-table-column>
        <el-table-column label="教师姓名" prop="teacherName" align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.Teacher?.name }}</template>
        </el-table-column>
        <el-table-column label="直播时间" prop="actionTime" align="center" width="170">
          <template #default="scope">{{ scope.row.classDay }} {{ scope.row.startTime }}</template>
        </el-table-column>
        <el-table-column label="分类" prop="topicName" align="center" show-overflow-tooltip>
          <template #default="scope">{{ CLASS_ID[scope.row.topicId] }}</template>
        </el-table-column>
        <el-table-column label="所属院系" prop="academyName" :minWidth='90' align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.Course?.Academy.name }}</template>
        </el-table-column>
        <el-table-column label="所属专业" prop="majorName" :minWidth='90' align="center" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.Course?.Major.name }}</template>
        </el-table-column>
        <el-table-column label="直播教室" prop="roomName" align="center" :minWidth="100" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.Room?.Name }}</template>
        </el-table-column>
        <el-table-column label="互动教室" prop="inRoom" align="center" :minWidth="100" show-overflow-tooltip>
<!--          <template #default="scope">{{ scope.row.roomName }}</template>-->
        </el-table-column>
        <el-table-column label="状态" prop="reason" align="center" :minWidth="100" show-overflow-tooltip>
          <template #default="scope">
            <div v-html="ISLEGALNODEMAP(scope)"></div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center"   width="220">
          <template #default="scope">
            <el-button link type="primary" @click="liveVideoPreviewRef.show(scope.row,true)">查看</el-button>
            <el-button link type="primary" :disabled="useStore.user.role !=1" @click="onTitleImgRow(scope.row)">更换标题封面</el-button>
          </template>
        </el-table-column>
        <el-table-column label="资源地址" width="180" prop="address" align="left" show-overflow-tooltip>
          <template #default="scope">{{ scope.row.address }}</template>
        </el-table-column>

      </el-table>
      <div class="mt-2">
        <el-pagination background v-model:currentPage="queryParam.page" v-model:page-size="queryParam.pageSize" :page-sizes="paginationOption.pageSizes" :layout="paginationOption.layout" :total="paginationOption.count" @size-change="handleChangeSize" @current-change="handleChangeCurrent" />
      </div>
    </div>
    <PassDialog ref="passDialogRef" @success="loadData()"></PassDialog>
    <DetailDialog ref="detailDialogRef" @success="loadData()"></DetailDialog>
    <TitleImgDialog ref="titleImgDialogRef" @success="loadData()"></TitleImgDialog>
    <LiveVideoPreview ref="liveVideoPreviewRef" ></LiveVideoPreview>
  </div>
</template>

<script setup>
/**
 * 录播资源管理
 */
import { ref, onMounted } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
import useAuthStore from '@/stores/useAuthStore';
import useUserStore from '@/stores/useUserStore';
import {pullingStateList, releaseStateList, reasonList, IS_LEGAL_NODE_MAP} from '@/apis/model/RecordModel';
import { findValue } from '@/utils/EsCommon';
//组件
import PassDialog from './PassDialog.vue';
import DetailDialog from './DetailDialog.vue';
import TitleImgDialog from './TitleImgDialog.vue';
import {CLASS_ID} from "@/apis/model/CourseModel";
import LiveVideoPreview from "@/components/video-preview/LiveVideoPreview.vue";

// ======================================================
//                      变量声明
// ======================================================

const pullingStateOption = ref(pullingStateList);
const releaseStateOption = ref(releaseStateList);
const reasonOption = ref(reasonList);
const liveVideoPreviewRef = ref(null);
const useStore = useUserStore();

// 组件引用
const radioFloor = ref('-1');
const detailDialogRef = ref(null);
const passDialogRef = ref(null);
const titleImgDialogRef = ref(null);

// 表格数据
const dataOfTable = ref([]);
// 表格查询参数
const queryParam = ref({
  pullingState: '',
  search: '',
  name: '',
  time: [],
  group_id: 0,
  page: 1,
  pageSize: 10,
})
// 分页栏配置数据
const paginationOption = ref({
  layout: 'total, sizes, prev, pager, next, jumper',
  pageSizes: [10, 20, 50, 100, 500, 1000],
  count: 0,
})
// 表格多选数据
const multipleSelection = ref([]);


// ======================================================
//                      处理事件
// ======================================================
const floorChange = () => {
  if (radioFloor.value == "-2") {
  } else if (radioFloor.value == "-3") {
  } else if (radioFloor.value == "-1") {
  } else {
  }
  onRefresh()
}
// 点击刷新按钮
const onRefresh = () => {
  queryParam.value.page = 1;
  loadData();
}

//点击重置按钮
const onReset = () => {
  queryParam.value = {
  pullingState: '',
  search: '',
  name: '',
  time: [],
  group_id: 0,
  page: 1,
  pageSize: 10,
}
  loadData()
}

// 处理表格全选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
// 跟换标题封面
const onTitleImgRow = (row = null) => {
    titleImgDialogRef.value.show(row.id, "更换标题封面", row);
}
// 点击行内编辑按钮
const onPass = (row) => {
  let n = multipleSelection.value.length, ids = multipleSelection.value.map(v => v.id).toString();
  if (n) {
    passDialogRef.value.show(ids, "审核不通过");
  } else {
    ElMessage.info("请选择批量数据");
  }
}
// 点击行内编辑按钮
const onEditRow = (row) => {
  detailDialogRef.value.show(row.id, "详情 ", row);
}


// 批量通过
const onRow = (row) => {
  let n = multipleSelection.value.length, ids = multipleSelection.value.map(v => v.id).toString();
  if (n) {
    ElMessageBox.confirm("确认审核通过？", "提示",{
      beforeClose: (action, instance, done) => {
        if (action === 'confirm') {
          instance.confirmButtonLoading = true
          EsAjax.post(ApiUrls.liveTaskChangeIsLegal, { id: ids,isLegal:1})
              .then(res => {
                if (res.data.code !== 200) {
                  ElMessage.warning(res.data.msg);
                  return;
                }
                ElMessage.success("操作成功");
                loadData();
              })
              .catch(err => { ElMessage.error("网络错误：", err.message) })
              .finally(()=>{instance.confirmButtonLoading = false;done();});
        } else {
          done()
        }
      }
    });
  } else {
    ElMessage.info("请选择批量数据");
  }
}

// 处理分页尺寸
const handleChangeSize = (val) => {
  console.log(`${val} items per page`);
  queryParam.value.page = 1;
  queryParam.value.pageSize = val;
  loadData();
}

// 处理页码
const handleChangeCurrent = (val) => {
  console.log(`current page: ${val}`)
  queryParam.value.page = val;
  loadData();
}

// ======================================================
//                      处理事件
// ======================================================

const loadData = () => {
  EsAjax.post(ApiUrls.liveTaskResourceList, queryParam.value).then(res => {
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list, count } = res.data.data
    dataOfTable.value = list;
    paginationOption.value.count = count;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

//状态修改
const ISLEGALNODEMAP =(scope)=>{
  if(scope.row.isLegal == 2) return `<span class="status-red">审核失败：${scope.row.Unlegal?.unlegalName}</span>`
  return IS_LEGAL_NODE_MAP[scope.row.isLegal]
}

onMounted(() => {
  loadData();
})

</script>
<style lang="less" scoped>
.class_name {
  position: absolute;
  font-size: 20px;
  left: 30px;
  top: 10px;
  font-weight: bold;
}
</style>

